/*
* Proyecto para la realizacion de las pruebas de las biblioteca StdTools y GMotor
*
* Este proyecto de pruebas requiere ademas de las bibliotecas SDL y SDL_Image
*/

//Librerias de sistema
#include <string>

using namespace std;

//Dependencias externas
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>


//Librerias de pruebas
#include <GMotor/GMotor.h>

using namespace GMotor;
using namespace GMotor::Video;

//Clases Adicionales
class Imagen : public Resource
{
private:
	SDL_Surface * surface;
	void load()
	{
		this->surface = IMG_Load(this->path.data());
	}

public:
	Imagen(string path):Resource(path)
	{
		this->surface = NULL;
	}
	~Imagen()
	{
		if (this->surface!=NULL)
			SDL_FreeSurface(this->surface);
	}

	SDL_Surface * getSurface()
	{
		return this->surface;
	}
};

class ImagenRegister : public Register
{
public:
	bool canLoad(string path)
	{
		bool encontrado = false;
		string ext = path.substr(path.find_last_of('.'));
		if (ext==".png")
			encontrado = true;
		return encontrado;
	}

	Resource * create(string path)
	{
		return new Imagen(path);
	}
};

class SDLRenderWindow : public RenderWindow
{
private:
	SDL_Surface * screen;
public:
	void createWindow(int width=800, int height=600, int bpp=32, bool fullscreen=false)
	{
		this->screen = SDL_SetVideoMode(width,height,bpp,SDL_HWSURFACE|SDL_DOUBLEBUF);
	}

	void swapBuffers()
	{
		SDL_Flip(this->screen);
	}
};

//Main
int main (int argc, char ** argv)
{
	SDL_Init(SDL_INIT_VIDEO);
	Root * root = new Root();
	ResourcesManager * rm = root->getResourcesManager();

	root->setWindow(new SDLRenderWindow());
	root->createWindow(800,600,32,false);

	rm->addRegister(new ImagenRegister());
	rm->addResource("images/engranaje.png","Inicio");
	rm->initializeGroup("Inicio");

	rm->addResource("images/imagen.png","Imagenes");
	rm->addResource("images/cuadrado.png","Imagenes");
	rm->initializeAll();

	SDL_Event evento;
	bool salir = false;

	while (!salir)
	{
		while(SDL_PollEvent(&evento))
		{
			switch (evento.type)
			{
			case SDL_QUIT:
				salir = true;
			case SDL_KEYDOWN:
				switch (evento.key.keysym.sym)
				{
				case SDLK_ESCAPE:
					salir = true;
				}
			}
		}
	}

	delete root;
	SDL_Quit();
	return 0;
}